﻿@model List<RoadQualityEvalAPI.Models.MapSectionModel>

@{
    var totalRegisteredMapSections = Model.Count;
}

<h4>Map Sections Administration:</h4>
<hr />


<script type="text/javascript">


    $('.mapSectionCreate').on('click', function (evt) {


        evt.preventDefault();
        evt.stopPropagation();

        //alert("Clicked");
        console.log('clicked');

        var $contentDiv = $('#content'), url = $(this).data('url');

        $.get(url, function (data) {

            $contentDiv.replaceWith(data);

        });

    });

    $('.mapSectionEdit').on('click', function (evt) {


        evt.preventDefault();
        evt.stopPropagation();

        //alert("Clicked");
        console.log('clicked');

        var $contentDiv = $('#content'), url = $(this).data('url');

        $.get(url, function (data) {

            $contentDiv.replaceWith(data);

        });

    });

    $('.mapSectionAssign').on('click', function (evt) {


        evt.preventDefault();
        evt.stopPropagation();

        //alert("Clicked");
        console.log('clicked');
        url = $(this).data('url');

        $.get(url, function (data) {

            alert("All potholes & speedbumps have been reassigned to their corresponding map sections!");
            location.reload();

        });

    });

</script>



<div id="map_sections_administration">

    <div class="row">
        <p>General info regarding map sections, how they affect the system functioning, etc.</p>
    </div>

    <div class="row">
        @Html.Label("Total registered map sections for server: " + totalRegisteredMapSections)
    </div>

    <div class="row">
        <button data-url='@Url.Action("CreateMapSection", "Admin")' class="mapSectionCreate">Create New Map Section</button>  
    </div>

    <div class="row">
        <button data-url='@Url.Action("ReassignPotholesSpeedbumpsToMapSections", "Admin")' class="mapSectionAssign">Reassign Potholes & Speedbumps To Map Sections</button>  
    </div>


    <div class="row">

        <table class="customTable">
            <tr>
                <th>
                    @Html.DisplayNameFor(m => m.FirstOrDefault().Name)
                </th>
                <th>
                    @Html.DisplayNameFor(m => m.FirstOrDefault().StreetName)
                </th>
                <th>
                    @Html.DisplayNameFor(m => m.FirstOrDefault().StartLocation)
                </th>
                <th>
                    @Html.DisplayNameFor(m => m.FirstOrDefault().EndLocation)
                </th>
                <th>
                    @Html.DisplayNameFor(m => m.FirstOrDefault().Description)
                </th>
                <th>
                    @Html.DisplayNameFor(m => m.FirstOrDefault().Potholes)
                </th>
                <th>
                    @Html.DisplayNameFor(m => m.FirstOrDefault().Speedbumps)
                </th>
                <th>
                    @Html.DisplayNameFor(m => m.FirstOrDefault().RoughnessQuality)
                </th>
                <th>
                    @Html.DisplayNameFor(m => m.FirstOrDefault().OverallQuality)
                </th>
                <th>
                    @Html.Raw("Actions")
                </th>
            </tr>

            @foreach (var mapSection in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(m => mapSection.Name)
                    </td>
                    <td>
                        @Html.DisplayFor(m => mapSection.StreetName)
                    </td>
                    <td>
                        @Html.DisplayFor(m => mapSection.StartLocation)
                    </td>
                    <td>
                        @Html.DisplayFor(m => mapSection.EndLocation)
                    </td>
                    <td>
                        @Html.DisplayFor(m => mapSection.Description)
                    </td>
                    <td>
                        @if (mapSection.Potholes != null && mapSection.Potholes.Count > 0)
                        {
                            <p>@mapSection.Potholes.Count</p>
                        }
                        else
                        {
                            <p>Not set</p>
                        }
                        
                    </td>
                    <td>
                        @if (mapSection.Speedbumps != null && mapSection.Speedbumps.Count > 0)
                        {
                            <p>@mapSection.Speedbumps.Count</p>
                        }
                        else
                        {
                            <p>Not set</p>
                        }
                    </td>
                    <td>
                        @Html.DisplayFor(m => mapSection.RoughnessQuality)
                    </td>
                    <td>
                        @Html.DisplayFor(m => mapSection.OverallQuality)
                    </td>
                    <td>
                         <button data-url='@Url.Action("EditMapSection", "Admin", new { name = mapSection.Name })' class="mapSectionEdit">Edit Map Section</button> |
                        @Html.ActionLink("Map Section Details", "MapSectionDetails", new {  }) |
                        @Html.ActionLink("Delete Map Section", "DeleteMapSection", new { id = mapSection.MapSectionId }, new { onclick = "return confirm('Are you sure you wish to delete this map section?');" })
                    </td>
                </tr> 
            }

        </table>

    </div>

</div>
